<?php

class Helper {
    const YES = 1;
    const NO = 0;

    public static function getYesNoOptions() {
        return array(
            self::NO => "No",
            self::YES => "Yes",
        );
    }

    public static function getYesNoText($key) {
        $yesNoOptions = self::getYesNoOptions();
        return isset($yesNoOptions[$key]) ? $yesNoOptions[$key] : "Unknown status ({$key})";
    }

    public static function getUserNameById($id) {
        return User::model()->findByPk($id)->name;
    }

    public static function getArticleCategoryNameById($id) {
        return (is_null(ArticleCategory::model()->findByPk($id)->name)) ? Constant::ROOT_NAME_CATEGORY : ArticleCategory::model()->findByPk($id)->name;
    }

    public static function getProductCategoryNameById($id) {
        return (is_null(ProductCategory::model()->findByPk($id)->name)) ? Constant::ROOT_NAME_CATEGORY : ProductCategory::model()->findByPk($id)->name;
    }

    public static function renderCrudToolTrashIncluded($controllerId, $primaryKeyString, $primaryKeyValue, $isDeleted) {
        $crudTool =
                '<div style="text-align:center">' .
                CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/add.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Add")), array("/" . $controllerId . "/create")) .
                CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/edit.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Edit")), array("/" . $controllerId . "/update", "id" => $primaryKeyValue));

        if ($isDeleted == 0) {
            $crudTool .= CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/recyclebin.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Trash")), array("#"), array('submit' => array('trash', 'id' => $primaryKeyValue), 'confirm' => 'Are you sure you want to trash this item?'));
        }
        else
            $crudTool .= CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/tick.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Untrash")), array("/" . $controllerId . "/untrash", 'id' => $primaryKeyValue));

        $crudTool .= CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/delete.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Delete")), array("#"), array('submit' => array('delete', 'id' => $primaryKeyValue), 'confirm' => 'Are you sure you want to delete this item?')) .
                '</div>';

        return $crudTool;
    }
    
    public static function renderCrudToolTrashIncludedNoAjax($controllerId, $primaryKeyString, $primaryKeyValue, $isDeleted) {
        $crudTool =
                '<div style="text-align:center">' .
                CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/add.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Add")), array("/" . $controllerId . "/create")) .
                CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/edit.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Edit")), array("/" . $controllerId . "/update", "id" => $primaryKeyValue));

        if ($isDeleted == 0) {
            $crudTool .= CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/recyclebin.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Trash")), array('trash', 'id' => $primaryKeyValue));
        }
        else
            $crudTool .= CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/tick.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Untrash")), array("/" . $controllerId . "/untrash", 'id' => $primaryKeyValue));

        $crudTool .= CHtml::link(CHtml::image(Yii::app()->request->baseUrl . '/images/delete.png', "", array("class" => "topDir mr5 ml5", "original-title" => "Delete")), array('delete', 'id' => $primaryKeyValue)) .
                '</div>';

        return $crudTool;
    }
    

    public static function checkActiveCurrentNavigation($controllerId, $navigationId) {
        if (strnatcasecmp($controllerId, $navigationId) == 0)
            return " active";
        else
            return "";
    }

    public static function checkDisplayBlockNavigation($controllerId, $navigationId) {
        if (strnatcasecmp($controllerId, $navigationId) == 0)
            return "display: block";
        else
            return "display: none";
    }

    public static function computePercentChanges($pastValue, $currentValue) {
        if ($pastValue == 0) // avoid dividing by zero
            $pastValue = 1;
        if ($currentValue == 0)
            $currentValue = 1;

        $changes = ($currentValue / $pastValue * 100) - 100;
        $changes = abs($changes);
        
        $roundedChanges = number_format($changes, 2) . "%";
        return $roundedChanges;
    }

    public static function getGainOrLost($pastValue, $currentValue) {
        if ($currentValue >= $pastValue)
            return "statPlus";
        else
            return "statMinus";
    }
    
    public static function buildingUserNames($userNames){
        return implode(",", $userNames);
    }
    
    public static function getDateTimeDifference($datetime1, $datetime2){
        echo $datetime1 . "<br/>";
        echo $datetime2 . "<br/>";
        $datetime1 = new DateTime($datetime1);
        $datetime2 = new DateTime($datetime2);
        $interval = $datetime1->diff($datetime2);
        
        echo "Difference " . $interval->y . " years, " .
                $interval->m . " months, " . $interval->d . 
                " days, " . $interval->h . " hours, " .
                $interval->i . " minutes " .
                $interval->s . " seconds ";
        echo "<br/>";
        echo $interval->format('%d days, %h hours, %i minutes, %s seconds');
    }

    
    public static function showPriceWithCurrency($price){
        return $price . " " . Yii::app()->params['currency'];
    }
}

?>
